/**
 * AbstractDao.java Copyright2011 PinMeal
 */
package com.pinmeal.persistent;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;

import android.os.Environment;

import com.pinmeal.types.PinMealParsingException;
import com.pinmeal.types.PinMealType;

/**
 * @author albertma Jul 19, 2011
 */
public abstract class AbstractFileLineDao
{

    protected static final String FILE_PATH = Environment.getExternalStorageDirectory() + "/PINMEAL/";

    protected void loadFromFile(String fileName)
    {

        parseFile(fileName);
    }

    protected byte[] parseFile(String fileName)
    {
        byte[] data = null;
        try
        {
            // let's parse it line by line.
            BufferedReader bufReader = new BufferedReader(new FileReader(FILE_PATH + fileName));
            String line = bufReader.readLine();
            while (line != null)
            {
                try
                {
                    parseLineDelegate(line);
                }
                catch(PinMealParsingException e)
                {
                    
                }
                line = bufReader.readLine();
            }

        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return data;
    }

    protected boolean storeToFile(Vector<? extends PinMealType> dataList, String fileName)
    {
        boolean isSuccess = false;
        int size = dataList.size();
        BufferedWriter bufWriter = null;
        String fullPath = FILE_PATH + fileName;
        try
        {
            File file = new File(fullPath);
            if (!file.exists())
            {
                createSubDirectory(FILE_PATH);
                file.createNewFile();
            }
            bufWriter = new BufferedWriter(new FileWriter(fullPath, false));
            for (int i = 0; i < size; i++)
            {
                PinMealType pinMealData = (PinMealType) dataList.elementAt(i);
                String line = storeLineDelegate(pinMealData);
                if (line == null)
                {
                    break;
                }
                if(i != 0)
                {
                    bufWriter.append('\n');
                }
                bufWriter.append(line);
            }
            bufWriter.flush();
            isSuccess = true;
        } catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally
        {
            if (bufWriter != null)
            {
                try
                {
                    bufWriter.close();
                } catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        return isSuccess;
    }


    private void createSubDirectory(String directory)
    {
        File dirPath = new File(directory);
        if(!dirPath.exists())
        {
            dirPath.mkdir();
        }
        
    }

    protected abstract void parseLineDelegate(String line) throws PinMealParsingException;

    protected abstract String storeLineDelegate(PinMealType record);
}
